package com.example.tired1.zyfypt_14120200131_syn.activity;

import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

import com.example.tired1.zyfypt_14120200131_syn.R;
import com.example.tired1.zyfypt_14120200131_syn.iface.RegisterListener;
import com.example.tired1.zyfypt_14120200131_syn.model.RegisterMode;

public class RegisterActivity extends AppCompatActivity implements RegisterListener,View.OnClickListener{
   EditText et_username, et_password,et_password1,et_tel;
    Button  btn_register,btn_return;
    Context context;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_register);
        initView();
        context=RegisterActivity.this;

    }

    private void initView() {
        et_password=(EditText)findViewById(R.id.et_password);
        et_username=(EditText)findViewById(R.id.et_username);
        et_password1=(EditText)findViewById(R.id.et_password1);
        et_tel=(EditText)findViewById(R.id.et_tel);
        btn_register=(Button)findViewById(R.id.btn_register);
        btn_return=(Button)findViewById(R.id.btn_return);
        btn_register.setOnClickListener(this);
        btn_return.setOnClickListener(this);


    }


    @Override
    public void onClick(View view) {//调用registermode方法
        switch (view.getId()){
            case R.id. btn_register:
                String username=et_username.getText().toString();
                String password=et_password.getText().toString();
                String password1=et_password1.getText().toString();
                String tel=et_tel.getText().toString();

                if(password.equals(password1)){
                    et_password1.setError("密码不一致，请重新输入");
                }
                RegisterMode mode=new RegisterMode();
                mode.register(username,password,tel,this);//this：监听器
                break;
            case R.id.btn_return:
                Intent intent=new Intent(context,LoginActivity.class);//发起跳转的一方
                startActivity(intent);//发出意图
                finish();//关掉注册的界面
                break;
        }

    }

    @Override
    public void onResponse(String msg) {//网络访问成功，进入执行
        if(msg.contains("1")) {//注册成功
            Toast.makeText(context,"注册成功，请返回登录",
                    Toast.LENGTH_SHORT).show();
        }
        else{
            Toast.makeText(context,"用户名已存在，请重新注册",
                    Toast.LENGTH_SHORT).show();

        }

    }

    @Override
    public void onfail(String s) {
            Toast.makeText(context,"s",
                    Toast.LENGTH_SHORT).show();


    }
}
